package com.yuntian.controller.admin;

import com.yuntian.Dto.UserPageQueryDto;
import com.yuntian.Vo.admin.UserVo;
import com.yuntian.entity.User;
import com.yuntian.result.PageResult;
import com.yuntian.result.Result;
import com.yuntian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

//对用户进行管理的Controller
@RestController("AdminUserController")
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户列表
     * @param userPageQueryDto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('USER_READ')")
    public Result<PageResult> List(UserPageQueryDto userPageQueryDto){
        PageResult pageResult = userService.selectUserList(userPageQueryDto);

        return Result.success(pageResult);
    }
    //统计用户数量
    @GetMapping("/count")
    public Result<Integer> count(){
        Integer count = userService.count();
        return Result.success(count);
    }

    /**
     * 根据id查询用户
     * @return
     */
    @GetMapping("/{userId}")
    @PreAuthorize("hasAuthority('USER_READ')")
    public Result<UserVo> getByUserId(@PathVariable Long userId){
        UserVo user = userService.getByUserId(userId);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('USER_WRITE')")
    public Result update(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }


    @PutMapping("/status")
    @PreAuthorize("hasRole('SUPER')")
    public Result startOrStop(@RequestBody User user){
        userService.startOrStop(user);
        return Result.success();
    }

}
